package cmdServer

import (
	"SortSet/sortset"
	"encoding/json"
	"fmt"
	"os"
	"strconv"
	"strings"
)

type command struct {
	// 提示,help指令输出
	tip string
	// 是否可以处理该输入
	canProcess func(str string) bool
	// 处理输入
	process func(str string, set *sortset.SortSet) string
}

var helpCommand = command{
	tip: "help:输出帮助信息",
	canProcess: func(str string) bool {
		return "help" == str
	},
	process: func(str string, set *sortset.SortSet) string {
		resultStr := ""
		for i, v := range commands {
			resultStr += fmt.Sprintf("%d: %s", i+1, v.tip+"\n")
		}
		fmt.Print(resultStr)
		return ""
	},
}

var dataCommand = command{
	tip: "data:输出当前SortSet数据",
	canProcess: func(str string) bool {
		return "data" == str
	},
	process: func(str string, set *sortset.SortSet) string {
		bs, _ := json.Marshal(set.Data())
		return string(bs)
	},
}

var addCommand = command{
	tip: "add:添加多个元素,json格式." +
		"\n\t例子1: 'add [\"hello world\", \"hello you \"]'",
	canProcess: func(str string) bool {
		return strings.HasPrefix(str, "add")
	},
	process: func(str string, set *sortset.SortSet) string {
		str = strings.ReplaceAll(str, "add", "")
		str = strings.TrimSpace(str)
		var input []string
		err := json.Unmarshal([]byte(str), &input)
		if err != nil {
			return err.Error()
		}
		set.AddAll(input...)
		return "success"
	},
}

var removeCommand = command{
	tip: "remove:删除多个元素,json格式." +
		"\n\t例子1: 'remove [\"hello world\", \"hello you \"]'",
	canProcess: func(str string) bool {
		return strings.HasPrefix(str, "remove")
	},
	process: func(str string, set *sortset.SortSet) string {
		str = strings.ReplaceAll(str, "remove", "")
		str = strings.TrimSpace(str)
		var input []string
		err := json.Unmarshal([]byte(str), &input)
		if err != nil {
			return err.Error()
		}
		set.RemoveAll(input...)
		return "success"
	},
}

var subCommand = command{
	tip: "sub:获取指定区间的集合;" +
		"\n\t例子1: 'sub 1:3' ,获取1-3的集合;" +
		"\n\t例子2: 'sub 1:', 获取1到结尾的集合;" +
		"\n\t例子3: 'sub :', 获取全部集合;" +
		"\n\t例子4: 'sub :3',获取开始到3的集合",
	canProcess: func(str string) bool {
		return strings.HasPrefix(str, "sub")
	},
	process: func(str string, set *sortset.SortSet) string {
		str = strings.ReplaceAll(str, "sub", "")
		str = strings.TrimSpace(str)
		strs := strings.Split(str, ":")
		if len(strs) != 2 {
			return "格式错误"
		}
		start, end := strings.TrimSpace(strs[0]), strings.TrimSpace(strs[1])
		var startI, endI int
		var err error
		if start == "" {
			startI = 0
		} else {
			startI, err = strconv.Atoi(start)
			if err != nil {
				return err.Error()
			}
		}
		if end == "" {
			endI = -1
		} else {
			endI, err = strconv.Atoi(end)
			if err != nil {
				return err.Error()
			}
		}
		cSlice, err := set.Sub(startI, endI)
		if err != nil {
			return err.Error()
		}
		bs, _ := json.Marshal(cSlice)
		return string(bs)
	},
}

var countCommand = command{
	tip: "count:返回元素的个数",
	canProcess: func(str string) bool {
		return strings.HasPrefix(str, "count")
	},
	process: func(str string, set *sortset.SortSet) string {

		return fmt.Sprint(set.Size())
	},
}

var intersectionCommand = command{
	tip: "intersection:交集，输出当前集合和输入集合的交集；" +
		"\n\t例子1： intersection [\"hello\", \"world\"]",
	canProcess: func(str string) bool {
		return strings.HasPrefix(str, "intersection")
	},
	process: func(str string, set *sortset.SortSet) string {
		str = strings.ReplaceAll(str, "intersection", "")
		str = strings.TrimSpace(str)
		var input []string
		err := json.Unmarshal([]byte(str), &input)
		if err != nil {
			return err.Error()
		}
		newSort := sortset.NewSortSet()
		newSort.AddAll(input...)
		resSort, err := set.Intersection(newSort)
		if err != nil {
			return err.Error()
		}
		bs, _ := json.Marshal(resSort.Data())
		return string(bs)
	},
}

var differenceCommand = command{
	tip: "difference:差集，输出当前集合和输入集合的差集；" +
		"\n\t例子1： difference [\"hello\", \"world\"]",
	canProcess: func(str string) bool {
		return strings.HasPrefix(str, "difference")
	},
	process: func(str string, set *sortset.SortSet) string {
		str = strings.ReplaceAll(str, "difference", "")
		str = strings.TrimSpace(str)
		var input []string
		err := json.Unmarshal([]byte(str), &input)
		if err != nil {
			return err.Error()
		}
		newSort := sortset.NewSortSet()
		newSort.AddAll(input...)
		resSort, err := set.Difference(newSort)
		if err != nil {
			return err.Error()
		}
		bs, _ := json.Marshal(resSort.Data())
		return string(bs)
	},
}

var unionCommand = command{
	tip: "union:并集，输出当前集合和输入集合的并集；" +
		"\n\t例子1： union [\"hello\", \"world\"]",
	canProcess: func(str string) bool {
		return strings.HasPrefix(str, "union")
	},
	process: func(str string, set *sortset.SortSet) string {
		str = strings.ReplaceAll(str, "union", "")
		str = strings.TrimSpace(str)
		var input []string
		err := json.Unmarshal([]byte(str), &input)
		if err != nil {
			return err.Error()
		}
		newSort := sortset.NewSortSet()
		newSort.AddAll(input...)
		resSort, err := set.Union(newSort)
		if err != nil {
			return err.Error()
		}
		bs, _ := json.Marshal(resSort.Data())
		return string(bs)
	},
}

var exitCommand = command{
	tip: "exit:退出程序.",
	canProcess: func(str string) bool {
		return strings.HasPrefix(str, "exit")
	},
	process: func(str string, set *sortset.SortSet) string {
		os.Exit(1)
		return ""
	},
}
